package com.university.report.common.utils;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.*;

import java.util.List;

/**
 * @ClassName health_report
 * @Author Jinondo
 * @Date 2021/6/12 19:40
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TitleSheetWriteHandler implements CellWriteHandler {

    //操作行
    private List<Integer> columnIndexs;
    //操作列
    private List<Integer> rowIndexs;
    //颜色
    private Short colorIndex;


    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 只处理第一行
        Sheet sheet = writeSheetHolder.getSheet();
        if( 0 == cell.getRowIndex()){
            // 设置列宽
            sheet.setColumnWidth(cell.getColumnIndex(), 20 * 256);
            // 设置行高
            sheet.getRow(0).setHeight((short)(3*256));
            // 获取workbook
            Workbook workbook = sheet.getWorkbook();
            // 获取样式实例
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            headWriteCellStyle.setWrapped(false);
            // 获取字体实例
            WriteFont headWriteFont = new WriteFont();
            // 设置字体样式
            headWriteFont.setFontName("宋体");
            // 设置字体大小
            headWriteFont.setFontHeightInPoints((short)14);
            // 边框
            headWriteFont.setBold(true);
            headWriteCellStyle.setWriteFont(headWriteFont);
            // 设置背景颜色为蓝色
            headWriteCellStyle.setFillPatternType(FillPatternType.SQUARES);
            headWriteCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
            if (CollectionUtils.isNotEmpty(columnIndexs) &&
                    CollectionUtils.isNotEmpty(rowIndexs) &&
                    colorIndex != null &&
                    cell.getRowIndex() == 0 &&
                    columnIndexs.contains(cell.getColumnIndex())) {
                // 设置指定单元格字体自定义颜色
                headWriteFont.setColor(colorIndex);
            }
            // 获取样式实例
            CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headWriteCellStyle);
            // 单元格设置样式
            cell.setCellStyle(cellStyle);
        }
        //不处理第一行
        if(0 != cell.getRowIndex()){
            // 根据单元格获取workbook
            Workbook workbook = cell.getSheet().getWorkbook();
            //设置行高
            sheet.getRow(cell.getRowIndex()).setHeight((short)(2*256));
            // 单元格策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            // 取消自动换行
            contentWriteCellStyle.setWrapped(false);
            // 设置背景颜色白色
            contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            // 设置垂直居中为居中对齐
            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            // 设置左右对齐为靠左对齐
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
            // 设置单元格上下左右边框为细边框
            contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
            contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
            contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
            contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
            // 创建字体实例
            WriteFont cellWriteFont = new WriteFont();
            // 设置字体大小
            cellWriteFont.setFontHeightInPoints((short)12);
            // 设置加粗
            cellWriteFont.setBold(false);
            if (CollectionUtils.isNotEmpty(columnIndexs) &&
                    CollectionUtils.isNotEmpty(rowIndexs) &&
                    colorIndex != null &&
                    rowIndexs.contains(cell.getRowIndex()) &&
                    columnIndexs.contains(cell.getColumnIndex())) {
                // 设置指定单元格字体颜色
                cellWriteFont.setColor(colorIndex);
            }
            contentWriteCellStyle.setWriteFont(cellWriteFont);
            CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, contentWriteCellStyle);
            cell.setCellStyle(cellStyle);
        }
    }
}
